﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace jnagr.Web
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Cookies["UserInfo"] != null)
                {
                    string name = Request.Cookies["UserInfo"]["LoginName"];
                    string pwd = Request.Cookies["UserInfo"]["Password"];
                    name = jnagr.Common.DEncrypt.DESEncrypt.Decrypt(name);
                    pwd = jnagr.Common.DEncrypt.DESEncrypt.Decrypt(pwd);
                    jnagr.BLL.userinfo u = new BLL.userinfo();
                    DataSet um = u.Login(name, pwd);
                    if (um.Tables[0].Rows.Count == 1)
                    {
                        jnagr.Model.userinfo userModel = u.Login(name, pwd, 1);
                        Session["user"] = um.Tables[0];
                        Session["UserInfo"] = userModel;
                        getlogininfo();
                    }

                }
            }                    
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            string name = txtName.Text;
            string pwd = txtPwd.Text;
            jnagr.BLL.userinfo u = new BLL.userinfo();
            DataSet um = u.Login(name, pwd);
            if (um.Tables[0].Rows.Count == 1)
            {
                jnagr.Model.userinfo userModel = u.Login(name, pwd, 1);
                Session["user"] = um.Tables[0];
                Session["UserInfo"] = userModel;
                if (chkpwd.Checked)
                {
                    HttpCookie userCookie = new HttpCookie("UserInfo");
                    userCookie.Expires = DateTime.Now.AddDays(15);
                    userCookie["UserID"] = jnagr.Common.DEncrypt.DESEncrypt.Encrypt(userModel.uid.ToString());
                    userCookie["DisplayName"] = jnagr.Common.DEncrypt.DESEncrypt.Encrypt(userModel.utruename);
                    userCookie["LoginName"] = jnagr.Common.DEncrypt.DESEncrypt.Encrypt(userModel.uname);
                    userCookie["Password"] = jnagr.Common.DEncrypt.DESEncrypt.Encrypt(userModel.upwd);
                    userCookie["Remark"] = jnagr.Common.DEncrypt.DESEncrypt.Encrypt(userModel.remark);
                    Response.Cookies.Add(userCookie);
                }
                getlogininfo();
                
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('用户名或密码错误！请重新登录！')", true);
            }
        }


        private void getlogininfo()
        {
            //lblmsg.Text = "您好，欢迎登录农业信息化服务平台！";
            int level = getLevel();
            string myarea = "";
            if (level>3)
                myarea = "全国";
            else
            {
                string[] p = getPCC();

                if (p[2] != "")
                {
                    myarea = p[2];
                }
                else if (p[1] != "")
                {
                    myarea = p[1];
                }
                else if (p[0] != "")
                {
                    myarea = p[0];
                }
            }
           

            lbladd.Text = myarea;
            lblpy.Text = jnagr.Common.PinYin.GetFirst(myarea) + "-";
            string umenu = getMenu();
            bool isshow = false;
            if (umenu.Contains("01"))
            {
                ImageButton1.Visible = true;
                isshow = true;
            }
            if (umenu.Contains("02"))
            {
                ImageButton2.Visible = true;
                isshow = true;
            }
            if (umenu.Contains("03"))
            {
                ImageButton3.Visible = true;
                isshow = true;
            }
            if (umenu.Contains("04"))
            {
                ImageButton4.Visible = true;
                isshow = true;
            }
            if (umenu.Contains("05"))
            {
                ImageButton5.Visible = true;
                isshow = true;
            }
            if (umenu.Contains("06"))
            {
                ImageButton6.Visible = true;
                isshow = true;
            }
            if (umenu.Contains("07"))
            {
                ImageButton7.Visible = true;
                isshow = true;
            }
            if (isshow)
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "$(function () {$('#win').fadeOut('slow');});", true);
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "$(function () {$.messager.alert('警告','您没有任何权限！');});", true);
            }
        }
        public string getMenu()
        {
            string umenu = "";
            DataTable dt = Session["user"] as DataTable;
            if (dt != null)
            {
                umenu = dt.Rows[0]["umenu"].ToString();
            }

            return umenu;
        }
        public int getLevel()
        {
            int id = 0;
            if (Session["user"] != null)
            {
                DataTable dt = Session["user"] as DataTable;
                if (dt != null)
                {
                    id = int.Parse(dt.Rows[0]["ulevel"].ToString());
                }
            }
            return id;
        }
        public string[] getPCC()
        {
            string pcc = "A,A,A";
            DataTable dt = Session["user"] as DataTable;
            if (dt != null)
            {
                pcc = dt.Rows[0]["uprovince"].ToString() + "," + dt.Rows[0]["ucity"].ToString() + "," + dt.Rows[0]["ucounty"].ToString();
            }
            return pcc.Split(',');
        }
        protected void ImageButton_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = sender as ImageButton;
            if (img != null)
            {
                if (Session["user"] != null)
                {

                    if (img.CommandName == "1")
                    {
                        string umenu = getMenu();
                        if (umenu.Contains("01s1"))
                        Response.Redirect("sicpet/default.aspx");
                        else if (umenu.Contains("01s2"))
                            Response.Redirect("sicpet/cxpic.aspx");
                        else if (umenu.Contains("01s3"))
                            Response.Redirect("weatherinfo/listcbd.aspx");
                        else if (umenu.Contains("01s4"))
                            Response.Redirect("forestpets/default.aspx");
                        else if (umenu.Contains("01s5"))
                            Response.Redirect("sicpet/Pic.aspx");                        
                    }
                    else if (img.CommandName == "2")
                    {
                        Response.Redirect("ecologyMonitor/default.aspx");
                    }
                    else if (img.CommandName == "3")
                    {
                        Response.Redirect("weatherinfo/list.aspx");
                    }
                    else if (img.CommandName == "4")
                    {
                        Response.Redirect("ground/default.aspx");
                    }
                    else if (img.CommandName == "5")
                    {
                        Response.Redirect("info/default.aspx");
                    }
                    else if (img.CommandName == "6")
                    {
                        string umenu = getMenu();
                        if (umenu.Contains("06s1"))
                            Response.Redirect("weatherinfo/listcbdstate.aspx");
                        else if (umenu.Contains("06s2"))
                            Response.Redirect("devinfo/default.aspx?devtype=YBQ");
                        else if (umenu.Contains("06s3"))
                            Response.Redirect("devinfo/default.aspx?devtype=XQH");
                        else if (umenu.Contains("06s4"))
                            Response.Redirect("devinfo/default.aspx?devtype=BPY");
                        else if (umenu.Contains("06s5"))
                            Response.Redirect("devinfo/default.aspx?devtype=TSQ");
                        else if (umenu.Contains("06s6"))
                            Response.Redirect("devinfo/default.aspx?devtype=CBD");  
                        
                    }
                    else if (img.CommandName == "7")
                    {
                        Response.Redirect("info/index.aspx");
                    }
                    else
                    {
                        string i = img.CommandName;
                        Response.Redirect("jnagrm.aspx?jurl=btn" + i);
                    }
                }
                else
                {
                    ImageButton1.Visible=false;
                    ImageButton2.Visible = false;
                    ImageButton3.Visible = false;
                    ImageButton4.Visible = false;
                    ImageButton5.Visible = false;
                    ImageButton6.Visible = false;
                    ImageButton7.Visible = false;
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "$(function () {$('#win').fadeIn('slow');});", true);

                }
            }
        }

    }
}